<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <meta name="description" content="A fully featured admin theme which can be used to build CRM, CMS, etc.">
    <meta name="author" content="Coderthemes">

    <link rel="stylesheet" href="/adminstatic/base/base.css"/>
    <script src="/adminstatic/base/baseCss.js"></script>

    <style type="text/css">
        html, body {
            background: #fff;
        }

        .layui-input-block {
            margin-left: 0;
        }
    </style>
</head>

<body class="fixed-left">

<!-- Begin page -->
<div id="wrapper" style="overflow: auto;">

    <div class="container">

        <div class="panel panel-default" style="box-shadow: none">
            <div class="panel-body">
                <form class="layui-form" action="">
                    <div id="inputForm" class="form-horizontal">
                        <div class="row" style="padding:0 20px">
                            <div class="form-group">
                                <div class="col-md-12">
                                    <div class="col-xs-6 col-md-4">
                                        <label for="status"> 关联分类</label>
                                        <div class="layui-form-item">
                                            <div class="layui-input-block">
                                                <select id="status" name="status" lay-verify="required"
                                                        lay-filter="status">
                                                    <% for(var i=0;i
                                                    <parent.length
                                                            ;i++){ %>
                                                        <option value="<%=parent[i].id%>"
                                                                data-title="<%=parent[i].title%>"><%=parent[i].title%>
                                                        </option>
                                                        <% } %>
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="form-group">
                                <div class="col-xs-12 col-md-4 ">
                                    <label for="title"><small class="notNullFlag">*</small> 分类名称</label>
                                    <input class="form-control" type="text" id="title" name="title"
                                           value="" required>
                                </div>
                            </div>

                            <div class="form-group" id="order-group">
                                <div class="col-xs-12 col-md-4 ">
                                    <label for="order"><small class="notNullFlag">*</small> 分类序号</label>
                                    <input class="form-control" type="number" id="order" name="order"
                                           value=""
                                           required maxlength="10">
                                </div>
                            </div>

                        </div>
                    </div>
                </form>
            </div>
            <div id="actions">
                <button type="button" class="layui-btn layui-btn-normal" id="submitBtn">保存</button>
                <button type="button" class="layui-btn layui-btn-primary" onclick="closeIframe()">取消</button>
            </div>
        </div><!-- panel-body -->
    </div> <!-- panel -->

</div> <!-- container -->

</div>
<!-- END wrapper -->

<script src="/adminstatic/base/base.js"></script>
<script type="text/javascript">
    //下拉选择分类
    var index;
    var text;//下拉选择内容
    layui.use('form', function () {
        var form = layui.form;
        index = $("#status option:selected").val();
        text = $("#status").find("option:selected").attr("data-title");
        console.log(index,text)
        form.on('select(status)', function (data) {
            //1:拿到select对象：
            index = $('#status').find('option:selected').val();
            text = $(data.elem).find("option:selected").attr("data-title");
            console.log(index, text)
        });
    });


    $("#submitBtn").click(function () {
        var title = $("input[name='title']").val();
        var order = $("input[name='order']").val();
        console.log(text)
        if (!vaildeParam(title)) {
            layer.msg("分类名称不能为空", {icon: 5});
            return;
        }

        if (!vaildeParam(order)) {
            layer.msg("分类序号不能为空", {icon: 5});
            return;
        }

        if (parseInt(order) <= 0){
            layer.msg("分类序号必须大于0", {icon: 5});
            return;
        }

        var api = '/admin/product_category/addCategory';
        var data = {
            "title": title,
            "order": order,
            "parentname": text,
            "parent": index,
        }

        postJSON(api, data, function (res) {
            cleanSuccess(res)
        })

    })
</script>
</body>
</html>